package com.nvn.washiodriver.activities;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nvn.washiodriver.R;
import com.nvn.washiodriver.Objects.Schedule;
import com.nvn.washiodriver.Objects.ScheduleItem;
import com.nvn.washiodriver.adapters.ScheduleListAdapter;
import com.nvn.washiodriver.network.WebServiceCommunicatorListener;
import com.nvn.washiodriver.network.ws.BaseWSControl;
import com.nvn.washiodriver.network.ws.BaseWSControl.WebServiceFlag;
import com.nvn.washiodriver.network.ws.GetScheduleListWSControl;
import com.parse.ParseUser;

public class ScheduleActivity extends BaseActivity implements OnClickListener,
		WebServiceCommunicatorListener {

	private ListView mScheduleList;
	private ScheduleListAdapter mAdapter;
	private Schedule mSchedule;
	private Button mBtnThisWeek, mBtnNextWeek;
	private ProgressDialog pd;
	private TextView mTvFullName;
	private ImageView mImvAvatar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_schedule);

		initView();
		initProperties();
		addListeners();

	}

	@Override
	protected void initView() {
		super.initView();
		mBtnThisWeek = (Button) findViewById(R.id.btn_this_week);
		mBtnNextWeek = (Button) findViewById(R.id.btn_next_week);
		mTvFullName = (TextView) findViewById(R.id.tv_driver_name);
		mImvAvatar = (ImageView) findViewById(R.id.imv_avatar);

		mScheduleList = (ListView) findViewById(R.id.list_schedules);
		mAdapter = new ScheduleListAdapter(this, new ArrayList<ScheduleItem>());

	}

	private void initProperties() {
		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		mScheduleList.setAdapter(mAdapter);
		loadData();
	}

	public void loadData() {
		String userObjectId = "";
		userObjectId = ParseUser.getCurrentUser().getObjectId();

		new GetScheduleListWSControl(this, this, userObjectId).execute();
	}

	private void addListeners() {
		mBtnThisWeek.setOnClickListener(this);
		mBtnNextWeek.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_this_week) {
			setCheckButton(0);
		}

		if (v.getId() == R.id.btn_next_week) {
			setCheckButton(1);
		}
	}

	private void setCheckButton(int pos) {
		if(mSchedule == null) return;
		
		if (pos == 0) {
			mBtnThisWeek.setSelected(true);
			mBtnNextWeek.setSelected(false);
			
			if(mSchedule.getThisWeeks().size() > 0){
				mScheduleList.setVisibility(View.VISIBLE);
				mAdapter.setData(mSchedule.getThisWeeks());
			}else{
				mScheduleList.setVisibility(View.GONE);
			}
		} else {
			mBtnThisWeek.setSelected(false);
			mBtnNextWeek.setSelected(true);
			
			if(mSchedule.getNextWeeks().size() > 0){
				mScheduleList.setVisibility(View.VISIBLE);
				mAdapter.setData(mSchedule.getNextWeeks());
			}else{
				mScheduleList.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		pd.setMessage("Loading...");
		pd.show();
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
		pd.dismiss();
	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		if (flag == WebServiceFlag.GET_SCHEDULE) {
			mSchedule = (Schedule) result;
			mTvFullName.setText(mSchedule.getName());
			setAvatar();
			setCheckButton(0);
		}
		pd.dismiss();
	}

	private void setAvatar() {
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.resetViewBeforeLoading().cacheOnDisc().cacheInMemory()
				.cacheOnDisc()
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.ARGB_8888)
				.showStubImage(R.drawable.placeholder_ninja)
				.showImageOnFail(R.drawable.placeholder_ninja)
				.showImageForEmptyUri(R.drawable.placeholder_ninja)
				.displayer(new SimpleBitmapDisplayer()).build();

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext())
				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 768))
				.memoryCacheSize(2 * 1024 * 1024)
				.imageDownloader(
						new BaseImageDownloader(getApplicationContext())) // default
				.tasksProcessingOrder(QueueProcessingType.FIFO) // default
				.defaultDisplayImageOptions(options) // default
				.enableLogging().build();
		ImageLoader.getInstance().init(config);
		
		ImageLoader.getInstance().loadImage(mSchedule.getProfileUrl().replace("https://", "http://"),
				new ImageLoadingListener() {

					@Override
					public void onLoadingStarted(String imageUri, View view) {
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						mImvAvatar.setImageBitmap(loadedImage);
					}

					@Override
					public void onLoadingCancelled(String imageUri, View view) {
					}
				});
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
	}

}
